package com.lin.csdn.controller;

import com.lin.csdn.service.impl.BrowseService;
import com.lin.csdn.service.impl.PicReportService;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/browseServlet")
public class BrowseServlet extends HttpServlet{
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * code：0抽取文本 GetArticle
     * 1抽取图片地址 GetPicUrl
     * 2收藏 Collect
     * 3关注 Attention
     * 4获取分享二维码 ShareQRCode
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String GetArticle="0";//获取博客文本
        String GetPicUrl="1";//获取图片地址
        String Collect="2";//执行收藏方法
        String Attention="3";//执行关注方法
        String ShareQRCode="4";//获取博客的分享二维码
        //获取req数据
        String blogId=req.getParameter("blogId");
        String result;

        BrowseService browseService=new BrowseService();
        PicReportService picReportService=new PicReportService();
        PrintWriter writer = resp.getWriter(); //响应流
        JSONObject object=new JSONObject();

        //根据需求调用各自的方法
        if(blogId.equals("0")) {
            writer.print(" ");//新建页面时，不返回数据
        }
        else{
            String code=req.getParameter("code");
            if (code.equals(GetArticle)) {
                result = browseService.getContext(blogId);
                object.put("context", result);
                writer.print(object);
            } else if (code.equals(GetPicUrl)) {
                result = browseService.getPicurl(blogId);
                object.put("picurl", result);
                writer.print(object);
            }
            else if(code.equals(Collect)){
                String userName=req.getParameter("userName");
                if(browseService.idCollect(blogId,userName)) {
                    browseService.collect(blogId, userName);
                    object.put("result", "收藏成功");
                }
                else
                object.put("result","请勿重复收藏");
                writer.print(object);
            }
            else if(code.equals(Attention)){
                String userName=req.getParameter("userName");
                if(browseService.idFollow(blogId,userName)) {
                    browseService.target(blogId, userName);
                    object.put("result", "关注成功");
                }
                else
                object.put("result","请勿重复关注");
                writer.print(object);
            }
            else if(code.equals(ShareQRCode)){
                result = picReportService.sharePic(blogId);
                object.put("picurl", result);
                writer.print(object);
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
